package com.sq.system.controller;


import com.alibaba.excel.EasyExcel;
import com.alibaba.excel.ExcelReader;
import com.alibaba.excel.read.listener.PageReadListener;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.sq.common.web.base.TableDataInfo;
import com.sq.common.web.domain.AjaxResult;
import com.sq.system.base.BaseController;
import com.sq.system.pojo.SysPile;
import com.sq.system.service.SysPileService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import java.io.File;
import java.io.IOException;
import java.io.Serializable;
import java.util.ArrayList;
import java.util.List;

/**
 * (SysPile)表控制层
 *
 * @author makejava
 * @since 2025-02-25 10:18:47
 */
@RestController
@RequestMapping("/api/pile")
public class SysPileController extends BaseController {
    /**
     * 服务对象
     */
    @Autowired
    private SysPileService sysPileService;

    /**
     * 分页查询所有数据
     *
     * @return 所有数据
     */
    @PostMapping("/listPage")
    public TableDataInfo selectAll(@RequestBody SysPile sysPile) {
        startPage(sysPile.getPageNum(), sysPile.getPageSize());
        return getDataTable(sysPileService.list(new QueryWrapper<SysPile>().orderByDesc("id").like("pile_name", "%" + sysPile.getPileName() + "%")));
    }

    /**
     * 通过主键查询单条数据
     *
     *
     * @return 单条数据
     */
//    @GetMapping("{id}")
//    public R selectOne(@PathVariable Serializable id) {
//        return success(this.sysPileService.getById(id));
//    }
    @PostMapping("/export")
    public AjaxResult export(@RequestParam("file") MultipartFile file) {
        return success(sysPileService.readExcelFile(file));
    }

    /**
     * 新增数据
     *
     * @param sysPile 实体对象
     * @return 新增结果
     */
    @PostMapping("/save")
    public AjaxResult insert(@RequestBody SysPile sysPile) {
        return success(this.sysPileService.save(sysPile));
    }

    /**
     * 修改数据
     *
     * @param sysPile 实体对象
     * @return 修改结果
     */
    @PutMapping
    public AjaxResult update(@RequestBody SysPile sysPile) {
        return success(this.sysPileService.updateById(sysPile));
    }

    /**
     * 删除数据
     *
     * @param idList 主键结合
     * @return 删除结果
     */
    @DeleteMapping
    public AjaxResult delete(@RequestParam("idList") List<Long> idList) {
        return success(this.sysPileService.removeByIds(idList));
    }
}

